{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Azure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Azure 1/单人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'd51d1e3e-05ca-4183-9906-680df2b8f39b',\n",
       "  'faceRectangle': {'top': 346, 'left': 407, 'width': 305, 'height': 305},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 19.0,\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.997,\n",
       "    'sadness': 0.002,\n",
       "    'surprise': 0.001},\n",
       "   'hair': {'bald': 0.1,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
       "     {'color': 'black', 'confidence': 0.85},\n",
       "     {'color': 'gray', 'confidence': 0.23},\n",
       "     {'color': 'blond', 'confidence': 0.19},\n",
       "     {'color': 'red', 'confidence': 0.14},\n",
       "     {'color': 'other', 'confidence': 0.08}]}}}]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入我们需要的模块\n",
    "import requests\n",
    "import json\n",
    "KEY = '415e308c1faf443da19e33cdbd30a6a3'  # Replace with a valid Subscription Key here.\n",
    "# Base URL,  Request URL中 符号?以前\n",
    "#。                          eastasia.api.cognitive.microsoft.com  ==》{endpoint}\n",
    "BASE_URL = 'https://api-fangwx.cognitiveservices.azure.com/face/v1.0/detect' # 人脸检测\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), #''  \n",
    "}\n",
    "\n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586791412303&di=ec4f61e828dd9c751121b88563094bc4&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201812%2F30%2F20181230222826_vxpys.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,hair,gender,glasses,emotion'), #年龄、头发、性别、眼镜、情感\n",
    "}\n",
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "# json.dumps()是将字典格式转换为json格式\n",
    "#age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise可选参数\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)#HTTP post请求 请求参数\n",
    "\n",
    "r.status_code#查看参数回传状态码\n",
    "results = r.json() #将回传数据转化为json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>秃头警告</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d51d1e3e-05ca-4183-9906-680df2b8f39b</th>\n",
       "      <td>女性</td>\n",
       "      <td>19.0</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.997</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄   眼镜   生气   蔑视   厌恶   恐惧   高兴  \\\n",
       "faceId                                                                         \n",
       "d51d1e3e-05ca-4183-9906-680df2b8f39b  女性  19.0  戴眼镜  0.0  0.0  0.0  0.0  0.0   \n",
       "\n",
       "                                         平静     伤心     惊讶  秃头警告  \n",
       "faceId                                                           \n",
       "d51d1e3e-05ca-4183-9906-680df2b8f39b  0.997  0.002  0.001   0.1  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.hair.bald\":\"秃头警告\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:-2]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"不戴眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Azure 2/双人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '86103b0b-4e47-4cb7-b8dd-39a565088ee5',\n",
       "  'faceRectangle': {'top': 120, 'left': 78, 'width': 75, 'height': 75},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 18.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.001,\n",
       "    'neutral': 0.986,\n",
       "    'sadness': 0.012,\n",
       "    'surprise': 0.0},\n",
       "   'hair': {'bald': 0.05,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'blond', 'confidence': 0.97},\n",
       "     {'color': 'red', 'confidence': 0.83},\n",
       "     {'color': 'brown', 'confidence': 0.6},\n",
       "     {'color': 'other', 'confidence': 0.49},\n",
       "     {'color': 'gray', 'confidence': 0.18},\n",
       "     {'color': 'black', 'confidence': 0.02}]}}},\n",
       " {'faceId': '9e15c5d4-aa79-4d87-b138-9de78a343098',\n",
       "  'faceRectangle': {'top': 121, 'left': 225, 'width': 73, 'height': 73},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0},\n",
       "   'hair': {'bald': 0.07,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 0.98},\n",
       "     {'color': 'brown', 'confidence': 0.94},\n",
       "     {'color': 'gray', 'confidence': 0.62},\n",
       "     {'color': 'other', 'confidence': 0.24},\n",
       "     {'color': 'blond', 'confidence': 0.05},\n",
       "     {'color': 'red', 'confidence': 0.03}]}}}]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入我们需要的模块\n",
    "import requests\n",
    "import json\n",
    "KEY = '415e308c1faf443da19e33cdbd30a6a3'  # Replace with a valid Subscription Key here.\n",
    "# Base URL,  Request URL中 符号?以前\n",
    "#。                          eastasia.api.cognitive.microsoft.com  ==》{endpoint}\n",
    "BASE_URL = 'https://api-fangwx.cognitiveservices.azure.com/face/v1.0/detect' # 人脸检测\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), #''  \n",
    "}\n",
    "\n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586792107949&di=885ead5d982113d24d1920cb7eb8f687&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F01%2F20181001194955_ryT4z.thumb.400_0.jpeg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,hair,gender,glasses,emotion'), #年龄、头发、性别、眼镜、情感\n",
    "}\n",
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "# json.dumps()是将字典格式转换为json格式\n",
    "#age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise可选参数\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)#HTTP post请求 请求参数\n",
    "\n",
    "r.status_code#查看参数回传状态码\n",
    "results = r.json() #将回传数据转化为json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>秃头警告</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>86103b0b-4e47-4cb7-b8dd-39a565088ee5</th>\n",
       "      <td>女性</td>\n",
       "      <td>18.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.986</td>\n",
       "      <td>0.012</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9e15c5d4-aa79-4d87-b138-9de78a343098</th>\n",
       "      <td>女性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.999</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜   生气   蔑视   厌恶   恐惧  \\\n",
       "faceId                                                                     \n",
       "86103b0b-4e47-4cb7-b8dd-39a565088ee5  女性  18.0  不戴眼镜  0.0  0.0  0.0  0.0   \n",
       "9e15c5d4-aa79-4d87-b138-9de78a343098  女性  20.0  不戴眼镜  0.0  0.0  0.0  0.0   \n",
       "\n",
       "                                         高兴     平静     伤心   惊讶  秃头警告  \n",
       "faceId                                                                \n",
       "86103b0b-4e47-4cb7-b8dd-39a565088ee5  0.001  0.986  0.012  0.0  0.05  \n",
       "9e15c5d4-aa79-4d87-b138-9de78a343098  0.000  0.999  0.001  0.0  0.07  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.hair.bald\":\"秃头警告\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:-2]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"不戴眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Azure 3/多人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '042e1e40-03f0-4ea1-8447-823791cc62d2',\n",
       "  'faceRectangle': {'top': 304, 'left': 672, 'width': 113, 'height': 113},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.001,\n",
       "    'neutral': 0.993,\n",
       "    'sadness': 0.004,\n",
       "    'surprise': 0.001},\n",
       "   'hair': {'bald': 0.39,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'red', 'confidence': 0.96},\n",
       "     {'color': 'other', 'confidence': 0.83},\n",
       "     {'color': 'brown', 'confidence': 0.57},\n",
       "     {'color': 'blond', 'confidence': 0.25},\n",
       "     {'color': 'black', 'confidence': 0.2},\n",
       "     {'color': 'gray', 'confidence': 0.07}]}}},\n",
       " {'faceId': '25b56458-3c17-48fd-8d17-170a1960ace6',\n",
       "  'faceRectangle': {'top': 303, 'left': 822, 'width': 111, 'height': 111},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.002,\n",
       "    'neutral': 0.997,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'red', 'confidence': 0.97},\n",
       "     {'color': 'other', 'confidence': 0.76},\n",
       "     {'color': 'brown', 'confidence': 0.72},\n",
       "     {'color': 'blond', 'confidence': 0.33},\n",
       "     {'color': 'black', 'confidence': 0.12},\n",
       "     {'color': 'gray', 'confidence': 0.05}]}}},\n",
       " {'faceId': '06a58919-14c0-4a1e-b61a-7dc564f21285',\n",
       "  'faceRectangle': {'top': 155, 'left': 377, 'width': 110, 'height': 110},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.003,\n",
       "    'happiness': 0.013,\n",
       "    'neutral': 0.827,\n",
       "    'sadness': 0.1,\n",
       "    'surprise': 0.057},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'red', 'confidence': 0.99},\n",
       "     {'color': 'other', 'confidence': 0.95},\n",
       "     {'color': 'black', 'confidence': 0.27},\n",
       "     {'color': 'brown', 'confidence': 0.26},\n",
       "     {'color': 'blond', 'confidence': 0.12},\n",
       "     {'color': 'gray', 'confidence': 0.04}]}}},\n",
       " {'faceId': 'd8816fe5-a455-497b-849a-b9b7ad4d2fc4',\n",
       "  'faceRectangle': {'top': 300, 'left': 307, 'width': 103, 'height': 103},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 21.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.653,\n",
       "    'neutral': 0.345,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.001},\n",
       "   'hair': {'bald': 0.1,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'red', 'confidence': 1.0},\n",
       "     {'color': 'other', 'confidence': 0.92},\n",
       "     {'color': 'brown', 'confidence': 0.48},\n",
       "     {'color': 'blond', 'confidence': 0.26},\n",
       "     {'color': 'black', 'confidence': 0.04},\n",
       "     {'color': 'gray', 'confidence': 0.03}]}}}]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入我们需要的模块\n",
    "import requests\n",
    "import json\n",
    "KEY = '415e308c1faf443da19e33cdbd30a6a3'  # Replace with a valid Subscription Key here.\n",
    "# Base URL,  Request URL中 符号?以前\n",
    "#。                          eastasia.api.cognitive.microsoft.com  ==》{endpoint}\n",
    "BASE_URL = 'https://api-fangwx.cognitiveservices.azure.com/face/v1.0/detect' # 人脸检测\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), #''  \n",
    "}\n",
    "\n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586792289776&di=4bd5481d3e6b25ac804b68b2e98b2feb&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201802%2F01%2F20180201145718_S3LxN.jpeg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,hair,gender,glasses,emotion'), #年龄、头发、性别、眼镜、情感\n",
    "}\n",
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "# json.dumps()是将字典格式转换为json格式\n",
    "#age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise可选参数\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)#HTTP post请求 请求参数\n",
    "\n",
    "r.status_code#查看参数回传状态码\n",
    "results = r.json() #将回传数据转化为json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>秃头警告</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>042e1e40-03f0-4ea1-8447-823791cc62d2</th>\n",
       "      <td>女性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.993</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25b56458-3c17-48fd-8d17-170a1960ace6</th>\n",
       "      <td>女性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.997</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>06a58919-14c0-4a1e-b61a-7dc564f21285</th>\n",
       "      <td>女性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.013</td>\n",
       "      <td>0.827</td>\n",
       "      <td>0.100</td>\n",
       "      <td>0.057</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d8816fe5-a455-497b-849a-b9b7ad4d2fc4</th>\n",
       "      <td>女性</td>\n",
       "      <td>21.0</td>\n",
       "      <td>不戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.653</td>\n",
       "      <td>0.345</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜   生气     蔑视   厌恶     恐惧  \\\n",
       "faceId                                                                         \n",
       "042e1e40-03f0-4ea1-8447-823791cc62d2  女性  20.0  不戴眼镜  0.0  0.001  0.0  0.000   \n",
       "25b56458-3c17-48fd-8d17-170a1960ace6  女性  20.0  不戴眼镜  0.0  0.000  0.0  0.000   \n",
       "06a58919-14c0-4a1e-b61a-7dc564f21285  女性  20.0  不戴眼镜  0.0  0.000  0.0  0.003   \n",
       "d8816fe5-a455-497b-849a-b9b7ad4d2fc4  女性  21.0  不戴眼镜  0.0  0.002  0.0  0.000   \n",
       "\n",
       "                                         高兴     平静     伤心     惊讶  秃头警告  \n",
       "faceId                                                                  \n",
       "042e1e40-03f0-4ea1-8447-823791cc62d2  0.001  0.993  0.004  0.001  0.39  \n",
       "25b56458-3c17-48fd-8d17-170a1960ace6  0.002  0.997  0.000  0.000  0.03  \n",
       "06a58919-14c0-4a1e-b61a-7dc564f21285  0.013  0.827  0.100  0.057  0.03  \n",
       "d8816fe5-a455-497b-849a-b9b7ad4d2fc4  0.653  0.345  0.000  0.001  0.10  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.hair.bald\":\"秃头警告\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:-2]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"不戴眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# face++ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#face++ 1/单人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586782887,0e509ecc-3328-4a5d-b89a-3440d6544d06',\n",
       " 'time_used': 1922,\n",
       " 'faces': [{'face_token': '6f7f234d42cb037313a42ca4268e6839',\n",
       "   'face_rectangle': {'top': 128, 'left': 80, 'width': 261, 'height': 261},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 24},\n",
       "    'smile': {'value': 1.595, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.017,\n",
       "     'disgust': 0.007,\n",
       "     'fear': 0.007,\n",
       "     'happiness': 0.007,\n",
       "     'neutral': 95.125,\n",
       "     'sadness': 0.017,\n",
       "     'surprise': 4.82}}}],\n",
       " 'image_id': 'xzT9aRDgm4UziaFx1qMVWw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"lDRQeFHx8nnrFQ9XG2RlAqOP_MV9E9Dm\"\n",
    "api_key = 'lF7NY3pIJ_yxxra_YHVPBuOFZFFgnY4R'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586792945317&di=7490684b3b1d3c6ee9228b25c1c4bd0b&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201612%2F10%2F20161210220334_ZP8tK.jpeg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6f7f234d42cb037313a42ca4268e6839</td>\n",
       "      <td>128</td>\n",
       "      <td>80</td>\n",
       "      <td>261</td>\n",
       "      <td>261</td>\n",
       "      <td>Male</td>\n",
       "      <td>24</td>\n",
       "      <td>1.595</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.007</td>\n",
       "      <td>95.125</td>\n",
       "      <td>0.017</td>\n",
       "      <td>4.82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  6f7f234d42cb037313a42ca4268e6839                 128                   80   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   261                    261                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    24                   1.595                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.017                       0.007   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.007                         0.007   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                      95.125                       0.017   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                         4.82  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#face++ 2/双人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586783037,ab5d94c7-ad72-4dc8-a998-74b68ab37cd8',\n",
       " 'time_used': 347,\n",
       " 'faces': [{'face_token': 'ec853bf0c8ccdbaac196151fa4517c8d',\n",
       "   'face_rectangle': {'top': 163, 'left': 406, 'width': 157, 'height': 157},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 6.3, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.002,\n",
       "     'disgust': 0.002,\n",
       "     'fear': 0.002,\n",
       "     'happiness': 0.028,\n",
       "     'neutral': 99.943,\n",
       "     'sadness': 0.021,\n",
       "     'surprise': 0.002}}},\n",
       "  {'face_token': '4fff378a1109187f770837800d31b0a6',\n",
       "   'face_rectangle': {'top': 203, 'left': 187, 'width': 148, 'height': 148},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 94.172, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.035,\n",
       "     'disgust': 1.59,\n",
       "     'fear': 7.948,\n",
       "     'happiness': 87.618,\n",
       "     'neutral': 1.336,\n",
       "     'sadness': 0.361,\n",
       "     'surprise': 1.113}}}],\n",
       " 'image_id': '0RvOI0uf/m+PA2MQwRhcgg==',\n",
       " 'face_num': 2}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"lDRQeFHx8nnrFQ9XG2RlAqOP_MV9E9Dm\"\n",
    "api_key = 'lF7NY3pIJ_yxxra_YHVPBuOFZFFgnY4R'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586793078018&di=66096906a57d4276f1842f7ff6b29c79&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20190901%2Feb71e8234408450eae829c41bc384cbe.jpeg'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ec853bf0c8ccdbaac196151fa4517c8d</td>\n",
       "      <td>163</td>\n",
       "      <td>406</td>\n",
       "      <td>157</td>\n",
       "      <td>157</td>\n",
       "      <td>Male</td>\n",
       "      <td>21</td>\n",
       "      <td>6.300</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.028</td>\n",
       "      <td>99.943</td>\n",
       "      <td>0.021</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4fff378a1109187f770837800d31b0a6</td>\n",
       "      <td>203</td>\n",
       "      <td>187</td>\n",
       "      <td>148</td>\n",
       "      <td>148</td>\n",
       "      <td>Female</td>\n",
       "      <td>21</td>\n",
       "      <td>94.172</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.035</td>\n",
       "      <td>1.590</td>\n",
       "      <td>7.948</td>\n",
       "      <td>87.618</td>\n",
       "      <td>1.336</td>\n",
       "      <td>0.361</td>\n",
       "      <td>1.113</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  ec853bf0c8ccdbaac196151fa4517c8d                 163                  406   \n",
       "1  4fff378a1109187f770837800d31b0a6                 203                  187   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   157                    157                    Male   \n",
       "1                   148                    148                  Female   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    21                   6.300                        50.0   \n",
       "1                    21                  94.172                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.002                       0.002   \n",
       "1                     0.035                       1.590   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.002                         0.028   \n",
       "1                    7.948                        87.618   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                      99.943                       0.021   \n",
       "1                       1.336                       0.361   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.002  \n",
       "1                        1.113  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#face++ 3/多人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586783278,59554ba2-7148-4103-b9b4-1f757b91355d',\n",
       " 'time_used': 1577,\n",
       " 'faces': [{'face_token': '4ad19768f4bfb65785c6dcaa87ff9e58',\n",
       "   'face_rectangle': {'top': 196, 'left': 153, 'width': 63, 'height': 63},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.735,\n",
       "     'disgust': 0.155,\n",
       "     'fear': 0.043,\n",
       "     'happiness': 99.024,\n",
       "     'neutral': 0.011,\n",
       "     'sadness': 0.022,\n",
       "     'surprise': 0.011}}},\n",
       "  {'face_token': 'cd15bbaf4a4ae24e061bbc4d55ba022d',\n",
       "   'face_rectangle': {'top': 184, 'left': 244, 'width': 56, 'height': 56},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.091,\n",
       "     'disgust': 0.772,\n",
       "     'fear': 0.008,\n",
       "     'happiness': 99.12,\n",
       "     'neutral': 0.003,\n",
       "     'sadness': 0.004,\n",
       "     'surprise': 0.003}}},\n",
       "  {'face_token': 'eed03e10b885c625172d01801d2c8793',\n",
       "   'face_rectangle': {'top': 203, 'left': 492, 'width': 56, 'height': 56},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 98.255, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.042,\n",
       "     'disgust': 1.165,\n",
       "     'fear': 0.109,\n",
       "     'happiness': 91.392,\n",
       "     'neutral': 5.175,\n",
       "     'sadness': 2.075,\n",
       "     'surprise': 0.042}}},\n",
       "  {'face_token': 'd45c9b468ce869e1fdc3d5fdd36fc3a6',\n",
       "   'face_rectangle': {'top': 171, 'left': 370, 'width': 52, 'height': 52},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 0.011, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.003,\n",
       "     'disgust': 0.003,\n",
       "     'fear': 0.068,\n",
       "     'happiness': 0.097,\n",
       "     'neutral': 99.434,\n",
       "     'sadness': 0.391,\n",
       "     'surprise': 0.003}}}],\n",
       " 'image_id': '8MkPUaLlMes2MaBUif7QNg==',\n",
       " 'face_num': 4}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"lDRQeFHx8nnrFQ9XG2RlAqOP_MV9E9Dm\"\n",
    "api_key = 'lF7NY3pIJ_yxxra_YHVPBuOFZFFgnY4R'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586793331833&di=bf72dbb30c671384783409c1bc1af0a3&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201808%2F23%2F20180823005832_qrljz.thumb.700_0.jpg'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4ad19768f4bfb65785c6dcaa87ff9e58</td>\n",
       "      <td>196</td>\n",
       "      <td>153</td>\n",
       "      <td>63</td>\n",
       "      <td>63</td>\n",
       "      <td>Male</td>\n",
       "      <td>23</td>\n",
       "      <td>100.000</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.735</td>\n",
       "      <td>0.155</td>\n",
       "      <td>0.043</td>\n",
       "      <td>99.024</td>\n",
       "      <td>0.011</td>\n",
       "      <td>0.022</td>\n",
       "      <td>0.011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>cd15bbaf4a4ae24e061bbc4d55ba022d</td>\n",
       "      <td>184</td>\n",
       "      <td>244</td>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>Male</td>\n",
       "      <td>21</td>\n",
       "      <td>100.000</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.091</td>\n",
       "      <td>0.772</td>\n",
       "      <td>0.008</td>\n",
       "      <td>99.120</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>eed03e10b885c625172d01801d2c8793</td>\n",
       "      <td>203</td>\n",
       "      <td>492</td>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>Male</td>\n",
       "      <td>20</td>\n",
       "      <td>98.255</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.042</td>\n",
       "      <td>1.165</td>\n",
       "      <td>0.109</td>\n",
       "      <td>91.392</td>\n",
       "      <td>5.175</td>\n",
       "      <td>2.075</td>\n",
       "      <td>0.042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d45c9b468ce869e1fdc3d5fdd36fc3a6</td>\n",
       "      <td>171</td>\n",
       "      <td>370</td>\n",
       "      <td>52</td>\n",
       "      <td>52</td>\n",
       "      <td>Male</td>\n",
       "      <td>22</td>\n",
       "      <td>0.011</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.068</td>\n",
       "      <td>0.097</td>\n",
       "      <td>99.434</td>\n",
       "      <td>0.391</td>\n",
       "      <td>0.003</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  4ad19768f4bfb65785c6dcaa87ff9e58                 196                  153   \n",
       "1  cd15bbaf4a4ae24e061bbc4d55ba022d                 184                  244   \n",
       "2  eed03e10b885c625172d01801d2c8793                 203                  492   \n",
       "3  d45c9b468ce869e1fdc3d5fdd36fc3a6                 171                  370   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                    63                     63                    Male   \n",
       "1                    56                     56                    Male   \n",
       "2                    56                     56                    Male   \n",
       "3                    52                     52                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    23                 100.000                        50.0   \n",
       "1                    21                 100.000                        50.0   \n",
       "2                    20                  98.255                        50.0   \n",
       "3                    22                   0.011                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.735                       0.155   \n",
       "1                     0.091                       0.772   \n",
       "2                     0.042                       1.165   \n",
       "3                     0.003                       0.003   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.043                        99.024   \n",
       "1                    0.008                        99.120   \n",
       "2                    0.109                        91.392   \n",
       "3                    0.068                         0.097   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       0.011                       0.022   \n",
       "1                       0.003                       0.004   \n",
       "2                       5.175                       2.075   \n",
       "3                      99.434                       0.391   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.011  \n",
       "1                        0.003  \n",
       "2                        0.042  \n",
       "3                        0.003  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 腾讯云"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#腾讯云 1/单人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 800,\n",
       "  'image_height': 1423,\n",
       "  'face_list': [{'face_id': '3593278927741681054',\n",
       "    'x': 256,\n",
       "    'y': 330,\n",
       "    'width': 293,\n",
       "    'height': 293,\n",
       "    'gender': 98,\n",
       "    'age': 21,\n",
       "    'expression': 42,\n",
       "    'beauty': 80,\n",
       "    'glass': 0,\n",
       "    'pitch': 13,\n",
       "    'yaw': -3,\n",
       "    'roll': 1,\n",
       "    'face_shape': {'face_profile': [{'x': 263, 'y': 421},\n",
       "      {'x': 264, 'y': 450},\n",
       "      {'x': 269, 'y': 478},\n",
       "      {'x': 277, 'y': 505},\n",
       "      {'x': 286, 'y': 532},\n",
       "      {'x': 299, 'y': 557},\n",
       "      {'x': 315, 'y': 581},\n",
       "      {'x': 333, 'y': 603},\n",
       "      {'x': 354, 'y': 622},\n",
       "      {'x': 378, 'y': 637},\n",
       "      {'x': 406, 'y': 643},\n",
       "      {'x': 434, 'y': 636},\n",
       "      {'x': 458, 'y': 620},\n",
       "      {'x': 479, 'y': 599},\n",
       "      {'x': 496, 'y': 575},\n",
       "      {'x': 510, 'y': 549},\n",
       "      {'x': 520, 'y': 522},\n",
       "      {'x': 526, 'y': 493},\n",
       "      {'x': 529, 'y': 463},\n",
       "      {'x': 530, 'y': 434},\n",
       "      {'x': 529, 'y': 408}],\n",
       "     'left_eye': [{'x': 301, 'y': 418},\n",
       "      {'x': 311, 'y': 425},\n",
       "      {'x': 322, 'y': 428},\n",
       "      {'x': 334, 'y': 428},\n",
       "      {'x': 345, 'y': 426},\n",
       "      {'x': 337, 'y': 415},\n",
       "      {'x': 325, 'y': 410},\n",
       "      {'x': 312, 'y': 411}],\n",
       "     'right_eye': [{'x': 463, 'y': 416},\n",
       "      {'x': 452, 'y': 423},\n",
       "      {'x': 440, 'y': 426},\n",
       "      {'x': 428, 'y': 426},\n",
       "      {'x': 416, 'y': 424},\n",
       "      {'x': 424, 'y': 413},\n",
       "      {'x': 437, 'y': 408},\n",
       "      {'x': 451, 'y': 409}],\n",
       "     'left_eyebrow': [{'x': 279, 'y': 389},\n",
       "      {'x': 297, 'y': 388},\n",
       "      {'x': 315, 'y': 390},\n",
       "      {'x': 332, 'y': 392},\n",
       "      {'x': 350, 'y': 394},\n",
       "      {'x': 336, 'y': 381},\n",
       "      {'x': 316, 'y': 376},\n",
       "      {'x': 296, 'y': 377}],\n",
       "     'right_eyebrow': [{'x': 483, 'y': 385},\n",
       "      {'x': 462, 'y': 386},\n",
       "      {'x': 442, 'y': 387},\n",
       "      {'x': 421, 'y': 390},\n",
       "      {'x': 401, 'y': 392},\n",
       "      {'x': 418, 'y': 378},\n",
       "      {'x': 440, 'y': 373},\n",
       "      {'x': 463, 'y': 372}],\n",
       "     'mouth': [{'x': 338, 'y': 547},\n",
       "      {'x': 354, 'y': 557},\n",
       "      {'x': 372, 'y': 564},\n",
       "      {'x': 391, 'y': 564},\n",
       "      {'x': 410, 'y': 560},\n",
       "      {'x': 426, 'y': 550},\n",
       "      {'x': 440, 'y': 537},\n",
       "      {'x': 421, 'y': 536},\n",
       "      {'x': 401, 'y': 534},\n",
       "      {'x': 388, 'y': 539},\n",
       "      {'x': 375, 'y': 537},\n",
       "      {'x': 356, 'y': 542},\n",
       "      {'x': 355, 'y': 548},\n",
       "      {'x': 372, 'y': 549},\n",
       "      {'x': 389, 'y': 550},\n",
       "      {'x': 406, 'y': 546},\n",
       "      {'x': 423, 'y': 542},\n",
       "      {'x': 423, 'y': 541},\n",
       "      {'x': 406, 'y': 545},\n",
       "      {'x': 389, 'y': 549},\n",
       "      {'x': 372, 'y': 548},\n",
       "      {'x': 355, 'y': 548}],\n",
       "     'nose': [{'x': 380, 'y': 494},\n",
       "      {'x': 379, 'y': 425},\n",
       "      {'x': 370, 'y': 444},\n",
       "      {'x': 361, 'y': 462},\n",
       "      {'x': 353, 'y': 480},\n",
       "      {'x': 342, 'y': 500},\n",
       "      {'x': 362, 'y': 511},\n",
       "      {'x': 382, 'y': 515},\n",
       "      {'x': 403, 'y': 510},\n",
       "      {'x': 424, 'y': 496},\n",
       "      {'x': 411, 'y': 477},\n",
       "      {'x': 400, 'y': 460},\n",
       "      {'x': 389, 'y': 442}]}}]}}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "#import cv2\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '1106649312' \n",
    "app_key = 'TwsQQv5G5c9E6FsH'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "\n",
    "with open(r\"\"\"C:\\Users\\Moonhee\\Pictures\\微信图片_20200413211921.jpg\"\"\",\"rb\") as f:\n",
    "    # b64encode是编码，b64decode是解码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "    # base64.b64decode(base64data)\n",
    "    \n",
    "# base64_data =pic_str\n",
    "\n",
    "#用opencv读入图片\n",
    "# frame=cv2.imread('e:/python/dlib/r3.jpg')\n",
    "# nparry_encode = cv2.imencode('.jpg', frame)[1]\n",
    "# data_encode = np.array(nparry_encode)\n",
    "# img = base64.b64encode(data_encode)    #得到API可以识别的字符串\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "#检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "#感受：腾讯云文档比较难读，与前面两家不同的是图片要求[本地图片]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "#腾讯云 2/双人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 1080,\n",
       "  'image_height': 1440,\n",
       "  'face_list': [{'face_id': '3594105894636375070',\n",
       "    'x': 555,\n",
       "    'y': 427,\n",
       "    'width': 459,\n",
       "    'height': 459,\n",
       "    'gender': 0,\n",
       "    'age': 15,\n",
       "    'expression': 65,\n",
       "    'beauty': 82,\n",
       "    'glass': 1,\n",
       "    'pitch': 15,\n",
       "    'yaw': -4,\n",
       "    'roll': 2,\n",
       "    'face_shape': {'face_profile': [{'x': 607, 'y': 599},\n",
       "      {'x': 609, 'y': 633},\n",
       "      {'x': 615, 'y': 666},\n",
       "      {'x': 624, 'y': 698},\n",
       "      {'x': 637, 'y': 730},\n",
       "      {'x': 654, 'y': 759},\n",
       "      {'x': 673, 'y': 787},\n",
       "      {'x': 695, 'y': 812},\n",
       "      {'x': 718, 'y': 837},\n",
       "      {'x': 742, 'y': 860},\n",
       "      {'x': 774, 'y': 871},\n",
       "      {'x': 813, 'y': 869},\n",
       "      {'x': 849, 'y': 853},\n",
       "      {'x': 882, 'y': 832},\n",
       "      {'x': 912, 'y': 807},\n",
       "      {'x': 938, 'y': 777},\n",
       "      {'x': 957, 'y': 742},\n",
       "      {'x': 969, 'y': 705},\n",
       "      {'x': 976, 'y': 666},\n",
       "      {'x': 981, 'y': 627},\n",
       "      {'x': 983, 'y': 592}],\n",
       "     'left_eye': [{'x': 641, 'y': 603},\n",
       "      {'x': 658, 'y': 601},\n",
       "      {'x': 676, 'y': 601},\n",
       "      {'x': 693, 'y': 601},\n",
       "      {'x': 711, 'y': 603},\n",
       "      {'x': 694, 'y': 601},\n",
       "      {'x': 676, 'y': 600},\n",
       "      {'x': 659, 'y': 601}],\n",
       "     'right_eye': [{'x': 904, 'y': 592},\n",
       "      {'x': 887, 'y': 599},\n",
       "      {'x': 869, 'y': 603},\n",
       "      {'x': 850, 'y': 603},\n",
       "      {'x': 832, 'y': 600},\n",
       "      {'x': 847, 'y': 587},\n",
       "      {'x': 866, 'y': 582},\n",
       "      {'x': 886, 'y': 583}],\n",
       "     'left_eyebrow': [{'x': 611, 'y': 557},\n",
       "      {'x': 638, 'y': 554},\n",
       "      {'x': 665, 'y': 553},\n",
       "      {'x': 693, 'y': 553},\n",
       "      {'x': 720, 'y': 552},\n",
       "      {'x': 694, 'y': 536},\n",
       "      {'x': 664, 'y': 533},\n",
       "      {'x': 634, 'y': 537}],\n",
       "     'right_eyebrow': [{'x': 939, 'y': 539},\n",
       "      {'x': 908, 'y': 539},\n",
       "      {'x': 877, 'y': 541},\n",
       "      {'x': 847, 'y': 546},\n",
       "      {'x': 817, 'y': 550},\n",
       "      {'x': 842, 'y': 528},\n",
       "      {'x': 875, 'y': 521},\n",
       "      {'x': 910, 'y': 521}],\n",
       "     'mouth': [{'x': 694, 'y': 761},\n",
       "      {'x': 712, 'y': 782},\n",
       "      {'x': 735, 'y': 798},\n",
       "      {'x': 762, 'y': 804},\n",
       "      {'x': 793, 'y': 802},\n",
       "      {'x': 823, 'y': 790},\n",
       "      {'x': 848, 'y': 771},\n",
       "      {'x': 815, 'y': 767},\n",
       "      {'x': 782, 'y': 759},\n",
       "      {'x': 761, 'y': 764},\n",
       "      {'x': 742, 'y': 757},\n",
       "      {'x': 718, 'y': 760},\n",
       "      {'x': 716, 'y': 769},\n",
       "      {'x': 739, 'y': 776},\n",
       "      {'x': 762, 'y': 781},\n",
       "      {'x': 791, 'y': 779},\n",
       "      {'x': 819, 'y': 776},\n",
       "      {'x': 819, 'y': 774},\n",
       "      {'x': 791, 'y': 776},\n",
       "      {'x': 762, 'y': 778},\n",
       "      {'x': 739, 'y': 773},\n",
       "      {'x': 717, 'y': 768}],\n",
       "     'nose': [{'x': 760, 'y': 703},\n",
       "      {'x': 766, 'y': 606},\n",
       "      {'x': 753, 'y': 632},\n",
       "      {'x': 741, 'y': 658},\n",
       "      {'x': 728, 'y': 684},\n",
       "      {'x': 714, 'y': 710},\n",
       "      {'x': 738, 'y': 726},\n",
       "      {'x': 762, 'y': 732},\n",
       "      {'x': 788, 'y': 727},\n",
       "      {'x': 817, 'y': 713},\n",
       "      {'x': 802, 'y': 685},\n",
       "      {'x': 790, 'y': 659},\n",
       "      {'x': 778, 'y': 633}]}},\n",
       "   {'face_id': '3594105900861770782',\n",
       "    'x': 203,\n",
       "    'y': 318,\n",
       "    'width': 380,\n",
       "    'height': 380,\n",
       "    'gender': 0,\n",
       "    'age': 23,\n",
       "    'expression': 47,\n",
       "    'beauty': 88,\n",
       "    'glass': 0,\n",
       "    'pitch': 14,\n",
       "    'yaw': 3,\n",
       "    'roll': -21,\n",
       "    'face_shape': {'face_profile': [{'x': 252, 'y': 389},\n",
       "      {'x': 242, 'y': 419},\n",
       "      {'x': 234, 'y': 450},\n",
       "      {'x': 230, 'y': 482},\n",
       "      {'x': 228, 'y': 514},\n",
       "      {'x': 232, 'y': 545},\n",
       "      {'x': 241, 'y': 576},\n",
       "      {'x': 254, 'y': 605},\n",
       "      {'x': 270, 'y': 632},\n",
       "      {'x': 291, 'y': 656},\n",
       "      {'x': 318, 'y': 673},\n",
       "      {'x': 348, 'y': 678},\n",
       "      {'x': 379, 'y': 673},\n",
       "      {'x': 408, 'y': 663},\n",
       "      {'x': 436, 'y': 649},\n",
       "      {'x': 462, 'y': 631},\n",
       "      {'x': 483, 'y': 609},\n",
       "      {'x': 502, 'y': 584},\n",
       "      {'x': 518, 'y': 558},\n",
       "      {'x': 533, 'y': 530},\n",
       "      {'x': 543, 'y': 504}],\n",
       "     'left_eye': [{'x': 301, 'y': 409},\n",
       "      {'x': 310, 'y': 421},\n",
       "      {'x': 323, 'y': 430},\n",
       "      {'x': 337, 'y': 434},\n",
       "      {'x': 352, 'y': 435},\n",
       "      {'x': 346, 'y': 420},\n",
       "      {'x': 333, 'y': 410},\n",
       "      {'x': 317, 'y': 406}],\n",
       "     'right_eye': [{'x': 494, 'y': 485},\n",
       "      {'x': 480, 'y': 487},\n",
       "      {'x': 466, 'y': 485},\n",
       "      {'x': 452, 'y': 478},\n",
       "      {'x': 441, 'y': 470},\n",
       "      {'x': 455, 'y': 463},\n",
       "      {'x': 471, 'y': 465},\n",
       "      {'x': 485, 'y': 472}],\n",
       "     'left_eyebrow': [{'x': 285, 'y': 360},\n",
       "      {'x': 311, 'y': 370},\n",
       "      {'x': 336, 'y': 381},\n",
       "      {'x': 361, 'y': 394},\n",
       "      {'x': 386, 'y': 405},\n",
       "      {'x': 370, 'y': 378},\n",
       "      {'x': 344, 'y': 361},\n",
       "      {'x': 315, 'y': 351}],\n",
       "     'right_eyebrow': [{'x': 533, 'y': 467},\n",
       "      {'x': 511, 'y': 455},\n",
       "      {'x': 488, 'y': 444},\n",
       "      {'x': 464, 'y': 436},\n",
       "      {'x': 440, 'y': 427},\n",
       "      {'x': 468, 'y': 419},\n",
       "      {'x': 496, 'y': 426},\n",
       "      {'x': 521, 'y': 441}],\n",
       "     'mouth': [{'x': 295, 'y': 558},\n",
       "      {'x': 299, 'y': 583},\n",
       "      {'x': 312, 'y': 606},\n",
       "      {'x': 335, 'y': 620},\n",
       "      {'x': 361, 'y': 624},\n",
       "      {'x': 386, 'y': 615},\n",
       "      {'x': 406, 'y': 598},\n",
       "      {'x': 389, 'y': 584},\n",
       "      {'x': 371, 'y': 571},\n",
       "      {'x': 354, 'y': 570},\n",
       "      {'x': 339, 'y': 560},\n",
       "      {'x': 317, 'y': 558},\n",
       "      {'x': 308, 'y': 574},\n",
       "      {'x': 324, 'y': 588},\n",
       "      {'x': 342, 'y': 599},\n",
       "      {'x': 364, 'y': 602},\n",
       "      {'x': 385, 'y': 602},\n",
       "      {'x': 388, 'y': 592},\n",
       "      {'x': 369, 'y': 586},\n",
       "      {'x': 350, 'y': 581},\n",
       "      {'x': 332, 'y': 572},\n",
       "      {'x': 314, 'y': 565}],\n",
       "     'nose': [{'x': 371, 'y': 529},\n",
       "      {'x': 396, 'y': 454},\n",
       "      {'x': 379, 'y': 470},\n",
       "      {'x': 362, 'y': 486},\n",
       "      {'x': 345, 'y': 502},\n",
       "      {'x': 324, 'y': 518},\n",
       "      {'x': 342, 'y': 538},\n",
       "      {'x': 362, 'y': 551},\n",
       "      {'x': 385, 'y': 554},\n",
       "      {'x': 411, 'y': 549},\n",
       "      {'x': 407, 'y': 524},\n",
       "      {'x': 403, 'y': 501},\n",
       "      {'x': 399, 'y': 477}]}}]}}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "#import cv2\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '1106649312' \n",
    "app_key = 'TwsQQv5G5c9E6FsH'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "\n",
    "with open(r\"\"\"C:\\Users\\Moonhee\\Pictures\\微信图片_20200414110141.jpg\"\"\",\"rb\") as f:\n",
    "    # b64encode是编码，b64decode是解码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "    # base64.b64decode(base64data)\n",
    "    \n",
    "# base64_data =pic_str\n",
    "\n",
    "#用opencv读入图片\n",
    "# frame=cv2.imread('e:/python/dlib/r3.jpg')\n",
    "# nparry_encode = cv2.imencode('.jpg', frame)[1]\n",
    "# data_encode = np.array(nparry_encode)\n",
    "# img = base64.b64encode(data_encode)    #得到API可以识别的字符串\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "#检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "#腾讯云 3/多人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 16397,\n",
       " 'msg': 'image size too big',\n",
       " 'data': {'image_width': 0, 'image_height': 0, 'face_list': []}}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "#import cv2\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '1106649312' \n",
    "app_key = 'TwsQQv5G5c9E6FsH'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "\n",
    "with open(r\"\"\"C:\\Users\\Moonhee\\Pictures\\微信图片_20200413211944.jpg\"\"\",\"rb\") as f:\n",
    "    # b64encode是编码，b64decode是解码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "    # base64.b64decode(base64data)\n",
    "    \n",
    "# base64_data =pic_str\n",
    "\n",
    "#用opencv读入图片\n",
    "# frame=cv2.imread('e:/python/dlib/r3.jpg')\n",
    "# nparry_encode = cv2.imencode('.jpg', frame)[1]\n",
    "# data_encode = np.array(nparry_encode)\n",
    "# img = base64.b64encode(data_encode)    #得到API可以识别的字符串\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "#检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#发现问题：会出现图片太大识别不了的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对比总结：\n",
    "#Azure  操作简单易懂，结果准确，最好\n",
    "#face++  检测结果可能会有一定偏差\n",
    "#腾讯云  还存在很多问题，操作起来也不方便，使用感不佳"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
